// 获取所属区域列表
function getRegion() {
    $.ajax({
        type: 'post',
        url: 'http://xdwy-develop.huijik.com/base/region_list',
        data: {},
        dataType: 'json',
        success: function (res) {
            let data = res.data
            for (let i = 0; i < data.length; i++) {
                $('[name=region]').append(`<option value="${data[i].id}">${data[i].name}</option>`)
            }
            upload()
        }
    })
}
getRegion()


// 获取项目名称
function getProject(index) {
    $.ajax({
        type: 'post',
        url: 'http://xdwy-develop.huijik.com/base/project_list',
        data: {
            region_id: index ? $("#region" + index).val() : $("#region").val()
        },
        dataType: 'json',
        success: function (res) {
            let data = res.data
            // 多个下拉
            if (index) {
                $('#project_id' + index).html('<option value="">全部</option>')
                for (let i = 0; i < data.length; i++) {
                    $('#project_id' + index).append(`<option value="${data[i].id}">${data[i].name}</option>`)
                }
            } else {
                // 单个
                $("#project_id").html('<option value="">全部</option>')
                for (let i = 0; i < data.length; i++) {
                    $("#project_id").append(`<option value="${data[i].id}">${data[i].name}</option>`)
                }
            }

            upload()
        }
    })
}

// 原数据
let classJson = []
let level = 1
// 联动id数组
let classJsonId = []
let lastClassJsonId = ''
// 获取五级联动
function getJson(type) {
    $.ajax({
        type: 'post',
        url: 'http://xdwy-develop.huijik.com/base/work_order_type_data_json',
        data: {
            work_order_type: type
        },
        dataType: 'json',
        success: function (res) {
            classJson = res.data
            classJsonChange(classJson, 0, 1)
        }
    })
}

// 联动
function classJsonChange(arr, id, level) {
    if (id == 0) {
        for (let i = 0; i < arr.length; i++) {
            $('#classJson' + level).append(`<option value="${arr[i].id}">${arr[i].name}</option>`)
        }
        upload()
    } else {
        if (arr != undefined) {
            for (let i = 0; i < arr.length; i++) {
                if (arr[i].id == id && arr[i].children) {
                    // 清空原数据
                    $('#classJson' + level).html('<option value="">全部</option>')
                    // 渲染
                    for (let j = 0; j < arr[i].children.length; j++) {
                        $('#classJson' + level).append(`<option value="${arr[i].children[j].id}">${arr[i].children[j].name}</option>`)
                    }
                    upload()
                } else {
                    classJsonChange(arr[i].children, id, level)
                }
            }
        }
    }
}

// 获取最后一级id
function getLastClassJsonId() {
    for (let i = 0; i < classJsonId.length; i++) {
        if (classJsonId[(classJsonId.length - 1) - i]) {
            return classJsonId[(classJsonId.length - 1) - i]
        }
    }
}

// layui初始化
function upload() {
    layui.use('form', function () {
        var form = layui.form;
        form.render()

        form.on('select(region)', function (data) {
            getProject($(data.elem).attr('data-index'))
        });

        form.on('select(classJson)', function (data) {
            let id = $(data.elem).val()
            level = Number($(data.elem).attr('data-level')) + 1 // 几级联动

            // 重置下拉 获取id
            for (let c = 0; c < $('.classJson').length; c++) {
                if ($(".classJson").eq(c).attr('data-level') > level - 1) {
                    $(".classJson").eq(c).html('<option value="">全部</option>')
                }

                classJsonId[c] = $(".classJson").eq(c).val()
            }

            if (data.value == '') {
                upload()
                return false
            }

            classJsonChange(classJson, id, level)
        });
    });
}
upload()